package com.dingdang.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dingdang.data.Result;
import com.dingdang.em.ResultEnum;
import com.dingdang.entity.PictureDetailsCar;
import com.dingdang.mapper.PictureDetailsCarMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author author
 * @since 2021-12-17
 */
@RestController
@RequestMapping("picture")
@Api(value = "后台图片")
public class PictureDetailsCarController {
    @Resource
    private PictureDetailsCarMapper pictureDetailsCarMapper;

    //查询全部图片信息
    @GetMapping("get")
    @ApiOperation(value = "查询全部图片信息")
    public Result getInfo() {
        try {
            List<PictureDetailsCar> pictures = pictureDetailsCarMapper.selectList(null);
            return Result.ok().setData(pictures);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.response(ResultEnum.ERROR).setData(e);
        }
    }

    //图片增加
    @PutMapping("add")
    @ApiOperation(value = "图片增加功能")
    @ApiImplicitParam(name = "pictureDetailsCar", value = "图片增加的实体类", required = true, paramType = "body")
    public Result addInfo(@RequestBody PictureDetailsCar pictureDetailsCar) {
        try {
            int insert = pictureDetailsCarMapper.insert(pictureDetailsCar);
            return Result.ok().setData(insert);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.response(ResultEnum.ERROR).setData(e);
        }
    }

    @PostMapping("update")
    @ApiOperation(value = "图片修改功能")
    @ApiImplicitParam(name = "pictureDetailsCar", value = "修改图片的实体类", required = true, paramType = "body")
    public Result updateInfo(@RequestBody PictureDetailsCar pictureDetailsCar) {
        try {
            int update = pictureDetailsCarMapper.updateById(pictureDetailsCar);
            if (update >= 1) {
                return Result.ok().setData(update);
            } else {
                return Result.response(ResultEnum.ERROR).setData(update);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.response(ResultEnum.ERROR).setData(e);
        }
    }

    //根据Id删除
    @DeleteMapping("delete")
    @ApiOperation(value = "图片删除")
    @ApiImplicitParam(name = "id", value = "要删除的Id", required = true, paramType = "query")
    public Result delete(@RequestParam("id") Long id) {
        try {
            int delete = pictureDetailsCarMapper.deleteById(id);
            if (delete != 0) {
                return Result.ok().setData(delete);
            } else {
                return Result.response(ResultEnum.ERROR).setData(delete);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.response(ResultEnum.ERROR).setData(e);
        }
    }

    //根据Id批量删除
    @DeleteMapping("deleteByIds")
    @ApiOperation(value = "图片批量删除")
    @ApiImplicitParam(name = "id", value = "要删除的Id", required = true, paramType = "query")
    public Result deleteIds(@RequestParam("ids") Long[] ids) {
        try {
            int delete = pictureDetailsCarMapper.deleteBatchIds(Arrays.asList(ids));
            if (delete != 0) {
                return Result.ok().setData(delete);
            } else {
                return Result.response(ResultEnum.ERROR).setData(delete);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.response(ResultEnum.ERROR).setData(e);
        }
    }
    @PostMapping("findList")
    @ApiOperation(value = "分页查询图片")
    public Result findList() {
        try {
            QueryWrapper<PictureDetailsCar> query = new QueryWrapper<>();
            Page<PictureDetailsCar> page = new Page<>(1, 2);
            IPage<PictureDetailsCar> pictures = pictureDetailsCarMapper.selectPage(page, query);
            List<PictureDetailsCar> records = pictures.getRecords();
            List<PictureDetailsCar> pictureDetailsCars = JSONObject.parseArray(JSONObject.toJSONString(records), PictureDetailsCar.class);
            return Result.ok().setData(pictureDetailsCars);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.response(ResultEnum.ERROR).setData(e);
        }
    }

}
